package org.example.juniortest;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

public class SchoolTool {
    static HashSet<String> names=new HashSet<String>();
    static Random random=new Random();

    public static String ramdomName(){
        String newName= String.valueOf((char)('a'+random.nextInt(26)))+String.valueOf((char)('a'+random.nextInt(26)))+String.valueOf((char)('a'+random.nextInt(26)));
        while(names.add(newName)){
            newName=String.valueOf((char)('a'+random.nextInt(26)))+String.valueOf((char)('a'+random.nextInt(26)))+String.valueOf((char)('a'+random.nextInt(26)));
        }
        return newName;
    }

    public static Integer randomAge(){
        return 18+random.nextInt(3);
    }

    public static BigDecimal randomHeight(){
        BigDecimal height = new BigDecimal(155+random.nextFloat()+random.nextInt(36));
        return height.setScale(2,BigDecimal.ROUND_HALF_UP);
    }

    public static BigDecimal randomWeight(){
        BigDecimal weight=new BigDecimal(45.1+random.nextInt(35)+random.nextFloat()+random.nextInt(6)/10);
        return weight.setScale(2,BigDecimal.ROUND_HALF_UP);
    }

    public static String randomClass(){
        int a=random.nextInt(5);
        switch (a) {
            case 0:return "一班";
            case 1:return "二班";
            case 2:return "三班";
            case 3:return "四班";
            case 4:return "五班";
        }
        return null;
    }

    public static Student createStudent(){
        Student student=new Student(ramdomName(),randomAge(),randomHeight(),randomWeight(),randomClass());
        return student;
    }

    public static Integer classNumber(List<Student> students){
        int one=0,two=0,three=0,four=0,five=0;
        Iterator<Student> iterator=students.iterator();
        while (iterator.hasNext()){
            switch (iterator.next().getClassroom()){
                case "一班":one=1;break;
                case "二班":two=1;break;
                case "三班":three=1;break;
                case "四班":four=1;break;
                case "五班":five=1;break;
            }
            if (one+two+three+four+five==5){
                return 5;
            }
        }
        return one+two+three+four+five;
    }

    public static Map<String,BigDecimal> classAverageWeight(List<Student> students){
        BigDecimal one=new BigDecimal(0),two=new BigDecimal(0),three=new BigDecimal(0),four=new BigDecimal(0),five=new BigDecimal(0);
        BigDecimal oneWeight=new BigDecimal(0),twoWeight=new BigDecimal(0),threeWeight=new BigDecimal(0),fourWeight=new BigDecimal(0),fiveWeight=new BigDecimal(0);
        HashMap<String,BigDecimal> hashMap=new HashMap<>();
        Iterator<Student> iterator=students.iterator();
        while (iterator.hasNext()){
            switch (iterator.next().getClassroom()){
                case "一班":
                    one=one.add(BigDecimal.valueOf(1));
                    oneWeight=oneWeight.add(iterator.next().getWeight());
                    break;
                case "二班":
                    two=two.add(BigDecimal.valueOf(1));
                    twoWeight=twoWeight.add(iterator.next().getWeight());
                    break;
                case "三班":
                    three=three.add(BigDecimal.valueOf(1));
                    threeWeight=threeWeight.add(iterator.next().getWeight());
                    break;
                case "四班":
                    four=four.add(BigDecimal.valueOf(1));
                    fourWeight=fourWeight.add(iterator.next().getWeight());
                    break;
                case "五班":
                    five=five.add(BigDecimal.valueOf(1));
                    fiveWeight=fiveWeight.add(iterator.next().getWeight());
                    break;
            }
        }
        hashMap.put("一班平均体重",oneWeight.divide(one,2, RoundingMode.HALF_UP));
        hashMap.put("二班平均体重",twoWeight.divide(two,2, RoundingMode.HALF_UP));
        hashMap.put("三班平均体重",threeWeight.divide(three,2, RoundingMode.HALF_UP));
        hashMap.put("四班平均体重",fourWeight.divide(four,2, RoundingMode.HALF_UP));
        hashMap.put("五班平均体重",fiveWeight.divide(five,2, RoundingMode.HALF_UP));
        return hashMap;
    }
}
